Skip to content

Add ggalluvial support #2044

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

moutikabdessabour
Copy link
Contributor

Closes #1614.

 p <- ggplot(as.data.frame(Titanic),
            aes(y = Freq,
                axis1 = Survived, axis2 = Sex, axis3 = Class)) +
        geom_alluvium(aes(fill = Class),
                width = 0, knot.pos = 0, reverse = FALSE) +
        guides(fill = "none") +
        geom_stratum(width = 1/8, reverse = FALSE) +
        geom_text(stat = "stratum", aes(label = after_stat(stratum)),
                  reverse = FALSE) +
        scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
        coord_flip() +
        ggtitle("Titanic survival by class and sex")
p
ggplotly(p)

ggplot

ggplotly

I added tests and everything.

transform_alluvium <- function(data) {
data <- data[order(data$x), ]

if(unique(data$colour) == 0) data$colour <- NULL
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what this is doing, but it seems you want length(), not unique()?

Copy link
Contributor Author

@moutikabdessabour moutikabdessabour Oct 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

geom_alluvium by default generates a data.frame with a colour column and sets it to 0. This behaviour results in an error when trying to get the colour from the number and grid::col2rgb complains that colors must be positive integers. I got around this by removing the colour column if no color aesthetic is passed by the user.

Error in grDevices::col2rgb(x, alpha = TRUE) : 
  numerical color values must be positive

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check needs to be more careful. If unique(data$colour) is not of length 1, the if will warn/error:

> if(c("a", "b") == 0) print("foo")
Warning message:
In if (c("a", "b") == 0) print("foo") :
  the condition has length > 1 and only the first element will be used

moutikabdessabour and others added 2 commits October 12, 2021 15:23
Co-authored-by: Carson Sievert <[email protected]>
Co-authored-by: Carson Sievert <[email protected]>
Comment on lines +20 to +46
test_that("using `geom_stratum` gives the correct output", {
p <- ggplot(as.data.frame(Titanic),
aes(y = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class)) +
geom_stratum(width = 1/8, reverse = FALSE) +
geom_text(stat = "stratum", aes(label = after_stat(stratum)),
reverse = FALSE) +
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
coord_flip() +
ggtitle("Titanic survival by class and sex")
#write_plotly_svg(p, "tests/testthat/_snaps/ggalluvial/stratum.svg")
expect_doppelganger(ggplotly(p), "stratum")
})

test_that("using `geom_alluvium` gives the correct output", {
p <- ggplot(as.data.frame(Titanic),
aes(y = Freq,
axis1 = Survived, axis2 = Sex, axis3 = Class)) +
geom_alluvium(aes(fill = Class),
width = 0, knot.pos = 0, reverse = FALSE) +
guides(fill = "none") +
scale_x_continuous(breaks = 1:3, labels = c("Survived", "Sex", "Class")) +
coord_flip() +
ggtitle("Titanic survival by class and sex")
#write_plotly_svg(p, "tests/testthat/_snaps/ggalluvial/alluvium.svg")
expect_doppelganger(ggplotly(p), "alluvium")
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests seems a bit redundant given you have them essentially combined in the plot above. It'd be better to have some coverage that we're handling colour/fill correctly when they're missing (or not a variable, like fill = "red")

Copy link
Contributor Author

@moutikabdessabour moutikabdessabour Oct 13, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so you want me to set fill to a static value in geom_alluvium. I think it works out of the box. Also for the checks to pass you need to add Suggests ggalluvial to NAMESPACE. so that pak would install it.

@cpsievert
Copy link
Collaborator

I'll take this over in #2061

@cpsievert cpsievert mentioned this pull request Nov 2, 2021
@cpsievert cpsievert closed this Nov 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

geom_GeomAlluvium() has yet to be implemented in plotly
2 participants